import mx.effects.easing.Bounce;
import mx.collections.ArrayCollection;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.events.ValidationResultEvent;            
import mx.utils.ObjectUtil;
import mx.controls.Image;
import mx.managers.PopUpManager;

private const LOAD_DELAY:int = 4;
private var timeElapsed:int = 0;
private var loadTimer:Timer;
public static var img:Image = new Image();
	
[Bindable] public var currencyList:ArrayCollection = new ArrayCollection(
	[	{label:"Albanian Lek (ALL)", data:"ALL"},
		{label:"Algerian Dinar (DZD)", data:"DZD"},
		{label:"Argentine Peso (ARS)", data:"ARS"},
		{label:"Aruba Florin (AWG)", data:"AWG"},
		{label:"Australian Dollar (AUD)", data:"AUD"},
		{label:"Bahamian Dollar (BSD)", data:"BSD"},
		{label:"Bahraini Dinar (BHD)", data:"BHD"},
		{label:"Bangladesh Taka (BDT)", data:"BDT"},
		{label:"Barbados Dollar (BBD)", data:"BBD"},
		{label:"Belize Dollar (BZD)", data:"BZD"},
		{label:"Bermuda Dollar (BMD)", data:"BMD"},
		{label:"Bhutan Ngultrum (BTN)", data:"BTN"},
		{label:"Bolivian Boliviano (BOB)", data:"BOB"},
		{label:"Botswana Pula (BWP)", data:"BWP"},
		{label:"Brazilian Real (BRL)", data:"BRL"},
		{label:"British Pound (GBP)", data:"GBP"},
		{label:"Brunei Dollar (BND)", data:"BND"},
		{label:"Burundi Franc (BIF)", data:"BIF"},
		{label:"Cambodia Riel (KHR)", data:"KHR"},
		{label:"Canadian Dollar (CAD)", data:"CAD"},
		{label:"Cape Verde Escudo (CVE)", data:"CVE"},
		{label:"Cayman Islands Dollar (KYD)", data:"KYD"},
		{label:"Chilean Peso (CLP)", data:"CLP"},
		{label:"Chinese Yuan (CNY)", data:"CNY"},
		{label:"Colombian Peso (COP)", data:"COP"},
		{label:"Comoros Franc (KMF)", data:"KMF"},
		{label:"Costa Rica Colon (CRC)", data:"CRC"},
		{label:"Croatian Kuna (HRK)", data:"HRK"},
		{label:"Cuban Peso (CUP)", data:"CUP"},
		{label:"Cyprus Pound (CYP)", data:"CYP"},
		{label:"Czech Koruna (CZK)", data:"CZK"},
		{label:"Danish Krone (DKK)", data:"DKK"},
		{label:"Dijibouti Franc (DJF)", data:"DJF"},
		{label:"Dominican Peso (DOP)", data:"DOP"},
		{label:"Egyptian Pound (EGP)", data:"EGP"},
		{label:"El Salvador Colon (SVC)", data:"SVC"},
		{label:"Estonian Kroon (EEK)", data:"EEK"},
		{label:"Ethiopian Birr (ETB)", data:"ETB"},
		{label:"Euro (EUR)", data:"EUR"},
		{label:"Gambian Dalasi (GMD)", data:"GMD"},
		{label:"Ghanian Cedi (GHC)", data:"GHC"},
		{label:"Gibraltar Pound (GIP)", data:"GIP"},
		{label:"Guatemala Quetzal (GTQ)", data:"GTQ"},
		{label:"Guinea Franc (GNF)", data:"GNF"},
		{label:"Guyana Dollar (GYD)", data:"GYD"},
		{label:"Haiti Gourde (HTG)", data:"HTG"},
		{label:"Honduras Lempira (HNL)", data:"HNL"},
		{label:"Hong Kong Dollar (HKD)", data:"HKD"},
		{label:"Hungarian Forint (HUF)", data:"HUF"},
		{label:"Iceland Krona (ISK)", data:"ISK"},
		{label:"Indian Rupee (INR)", data:"INR"},
		{label:"Indonesian Rupiah (IDR)", data:"IDR"},
		{label:"Iraqi Dinar (IQD)", data:"IQD"},
		{label:"Israeli Shekel (ILS)", data:"ILS"},
		{label:"Jamaican Dollar (JMD)", data:"JMD"},
		{label:"Japanese Yen (JPY)", data:"JPY"},
		{label:"Jordanian Dinar (JOD)", data:"JOD"},
		{label:"Kazakhstan Tenge (KZT)", data:"KZT"},
		{label:"Kenyan Shilling (KES)", data:"KES"},
		{label:"Kuwaiti Dinar (KWD)", data:"KWD"},
		{label:"Lao Kip (LAK)", data:"LAK"},
		{label:"Latvian Lat (LVL)", data:"LVL"},
		{label:"Lebanese Pound (LBP)", data:"LBP"},
		{label:"Lesotho Loti (LSL)", data:"LSL"},
		{label:"Liberian Dollar (LRD)", data:"LRD"},
		{label:"Libyan Dinar (LYD)", data:"LYD"},
		{label:"Lithuanian Lita (LTL)", data:"LTL"},
		{label:"Macau Pataca (MOP)", data:"MOP"},
		{label:"Macedonian Denar (MKD)", data:"MKD"},
		{label:"Malagasy Franc (MGF)", data:"MGF"},
		{label:"Malawi Kwacha (MWK)", data:"MWK"},
		{label:"Malaysian Ringgit (MYR)", data:"MYR"},
		{label:"Maldives Rufiyaa (MVR)", data:"MVR"},
		{label:"Maltese Lira (MTL)", data:"MTL"},
		{label:"Mauritania Ougulya (MRO)", data:"MRO"},
		{label:"Mauritius Rupee (MUR)", data:"MUR"},
		{label:"Mexican Peso (MXN)", data:"MXN"},
		{label:"Moldovan Leu (MDL)", data:"MDL"},
		{label:"Mongolian Tugrik (MNT)", data:"MNT"},
		{label:"Moroccan Dirham (MAD)", data:"MAD"},
		{label:"Mozambique Metical (MZM)", data:"MZM"},
		{label:"Myanmar Kyat (MMK)", data:"MMK"},
		{label:"Namibian Dollar (NAD)", data:"NAD"},
		{label:"Nepalese Rupee (NPR)", data:"NPR"},
		{label:"Neth Antilles Guilder (ANG)", data:"ANG"},
		{label:"New Zealand Dollar (NZD)", data:"NZD"},
		{label:"Nicaragua Cordoba (NIO)", data:"NIO"},
		{label:"Nigerian Naira (NGN)", data:"NGN"},
		{label:"North Korean Won (KPW)", data:"KPW"},
		{label:"Norwegian Krone (NOK)", data:"NOK"},
		{label:"Omani Rial (OMR)", data:"OMR"},
		{label:"Pakistani Rupee (PKR)", data:"PKR"},
		{label:"Panama Balboa (PAB)", data:"PAB"},
		{label:"Papua New Guinea Kina (PGK)", data:"PGK"},
		{label:"Paraguayan Guarani (PYG)", data:"PYG"},
		{label:"Peruvian Nuevo Sol (PEN)", data:"PEN"},
		{label:"Philippine Peso (PHP)", data:"PHP"},
		{label:"Polish Zloty (PLN)", data:"PLN"},
		{label:"Qatar Rial (QAR)", data:"QAR"},
		{label:"Romanian Leu (ROL)", data:"ROL"},
		{label:"Russian Rouble (RUB)", data:"RUB"},
		{label:"Samoa Tala (WST)", data:"WST"},
		{label:"Sao Tome Dobra (STD)", data:"STD"},
		{label:"Saudi Arabian Riyal (SAR)", data:"SAR"},
		{label:"Seychelles Rupee (SCR)", data:"SCR"},
		{label:"Sierra Leone Leone (SLL)", data:"SLL"},
		{label:"Singapore Dollar (SGD)", data:"SGD"},
		{label:"Slovak Koruna (SKK)", data:"SKK"},
		{label:"Slovenian Tolar (SIT)", data:"SIT"},
		{label:"Solomon Islands Dollar (SBD)", data:"SBD"},
		{label:"Somali Shilling (SOS)", data:"SOS"},
		{label:"South Korean Won (KRW)", data:"KRW"},
		{label:"South African Rand (ZAR)", data:"ZAR"},
		{label:"Sri Lanka Rupee (LKR)", data:"LKR"},
		{label:"Sudanese Dinar (SDD)", data:"SDD"},
		{label:"Surinam Guilder (SRG)", data:"SRG"},
		{label:"Swaziland Lilageni (SZL)", data:"SZL"},
		{label:"Swedish Krona (SEK)", data:"SEK"},
		{label:"Swiss Franc (CHF)", data:"CHF"},
		{label:"Syrian Pound (SYP)", data:"SYP"},
		{label:"Turkey Lira (TRY)", data:"TRY"},
		{label:"Taiwan Dollar (TWD)", data:"TWD"},
		{label:"Tanzanian Shilling (TZS)", data:"TZS"},
		{label:"Thai Baht (THB)", data:"THB"},
		{label:"Tonga Paanga (TOP)", data:"TOP"},
		{label:"Trinidad & Tobago Dollar (TTD)", data:"TTD"},
		{label:"Tunisian Dinar (TND)", data:"TND"},
		{label:"Turkish Lira (TRL)", data:"TRL"},
		{label:"U.S. Dollar (USD)", data:"USD"},
		{label:"UAE Dirham (AED)", data:"AED"},
		{label:"Ugandan Shilling (UGX)", data:"UGX"},
		{label:"Ukraine Hryvnia (UAH)", data:"UAH"},
		{label:"Uruguayan New Peso (UYU)", data:"UYU"},
		{label:"Vanuatu Vatu (VUV)", data:"VUV"},
		{label:"Venezuelan Bolivar (VEB)", data:"VEB"},
		{label:"Vietnam Dong (VND)", data:"VND"},
		{label:"Yemen Riyal (YER)", data:"YER"},
		{label:"Zambian Kwacha (ZMK)", data:"ZMK"},
		{label:"Zimbabwe Dollar (ZWD)", data:"ZWD"}   ]);

	
	[Bindable] private var thesaurusList:XMLList;
	[Bindable] private var thesaurusHelpLabelText:String;
	[Bindable] public var list:XMLList;
	[Bindable] public var zipcodeLocation:XMLList;
	
	protected function fromCurrencyComboBox_changeHandler(event:ListEvent):void
	{
		fromFlagImage.source = "assets/flagicons/"+ ComboBox(event.target).selectedItem.data +".png";
	}
	
	
	protected function toCurrencyComboBox_changeHandler(event:ListEvent):void
	{
		toFlagImage.source = "assets/flagicons/"+ ComboBox(event.target).selectedItem.data +".png";
	}
	
	protected function convertCurrencyButton_clickHandler(event:MouseEvent):void
	{
		if(currencyAmount.text == "")
		{
			Alert.show("Please enter some amount to be converted !!","Error");	
		}
		else
		{
			remoteObj.getCurrencyConversion(fromCurrencyComboBox.selectedItem.data,toCurrencyComboBox.selectedItem.data,currencyAmount.text);
		}
	}
	
	protected function clearCurrencyButton_clickHandler(event:MouseEvent):void
	{
		currencyAmount.text = "";
		currencyRate.text = "";
		currencyResult.text = "";
		fromFlagImage.source = "assets/flagicons/ALL.png";
		fromCurrencyComboBox.selectedIndex = 0;
		toFlagImage.source = "assets/flagicons/ALL.png";
		toCurrencyComboBox.selectedIndex = 0;				
		clearCurrencyButton.enabled = false;
	}
	
	private function currencyConversionResultHandler(event:ResultEvent):void
	{
		if(event.result!=null){
			var resultString:String = event.result as String;
			var params:Array = resultString.split("-", 2);
			
			currencyRate.text = params[0];
			currencyResult.text = params[1];
			clearCurrencyButton.enabled = true;
		}
		else{
			Alert.show("This service isn't functioning at this momemt. Please try again later!","Info");
			
			currencyAmount.text = "";
			currencyRate.text = "";
			currencyResult.text = "";
			fromFlagImage.source = "assets/flagicons/ALL.png";
			fromCurrencyComboBox.selectedIndex = 0;
			toFlagImage.source = "assets/flagicons/ALL.png";
			toCurrencyComboBox.selectedIndex = 0;				
			clearCurrencyButton.enabled = false;
		}
		
	}
	
	private function currencyConversionFaultHandler(event:FaultEvent):void
	{
		Alert.show( ObjectUtil.toString(event.fault) );
	}

	protected function getQuoteButton_clickHandler(event:MouseEvent):void
	{
		if(quoteSymbol.text == "")
		{
			Alert.show("Please enter a NASDAQ recognized symbol to get Quote results !!","Error");	
		}
		else
		{
			remoteObj.getStockQuote(quoteSymbol.text);
		}
	}

	protected function clearQuoteButton_clickHandler(event:MouseEvent):void
	{
		stockQuoteResult.text = "";
		quoteSymbol.text = "";
		clearQuoteButton.enabled = false;
	}
	
	private function quoteResultHandler(event:ResultEvent):void
	{
		if(event.result!=null){
			stockQuoteResult.text = stockQuoteResult.text + event.result.toString();
			clearQuoteButton.enabled = true;
		}
		else{
			Alert.show("This service isn't functioning at this momemt. Please try again later!","Info");
			
			stockQuoteResult.text = "";
			quoteSymbol.text = "";
			clearQuoteButton.enabled = false;
		}
	}
	
	private function quoteFaultHandler(event:FaultEvent):void
	{
		Alert.show( ObjectUtil.toString(event.fault) );
	}
	
	
	protected function defineDictionaryButton_clickHandler(event:MouseEvent):void
	{
		if(wordToDefine.text == "")
		{
			Alert.show("Please type any word to define !!","Error");	
		}
		else
		{
			remoteObj.getDefinition(wordToDefine.text);
		}
	}
	
	protected function clearDictionaryButton_clickHandler(event:MouseEvent):void
	{
		definitionField.text = "";
		wordToDefine.text = "";
		thesaurusDataGrid.dataProvider = "";
		thesaurusHelpLabelText = "";
		clearDictionaryButton.enabled = false;				
	}
	
	private function defineResultHandler(event:ResultEvent):void
	{
		if(event.result!=null){
			var definition:String = event.result as String; 
			definitionField.text = definition;
			
			remoteObj.getThesaurus(wordToDefine.text);
		}
		else{
			Alert.show("This service isn't functioning at this momemt. Please try again later!","Info");
			
			definitionField.text = "";
			wordToDefine.text = "";
			thesaurusDataGrid.dataProvider = "";
			thesaurusHelpLabelText = "";
			clearDictionaryButton.enabled = false;	
			
		}
	}
	
	private function defineFaultHandler(event:FaultEvent):void
	{
		Alert.show( ObjectUtil.toString(event.fault) );
	}
	
	private function thesaurusResultHandler(event:ResultEvent):void
	{
		if(event.result!=null){
			var xml:XML = new XML(event.result);
			thesaurusList = xml.word;
			thesaurusHelpLabelText = "syn:Synonym ant:Antonym rel:Related sim:Similar";
			clearDictionaryButton.enabled = true;
		}
		else{
			Alert.show("This service isn't functioning at this momemt. Please try again later!","Info");
			
			definitionField.text = "";
			wordToDefine.text = "";
			thesaurusDataGrid.dataProvider = "";
			thesaurusHelpLabelText = "";
			clearDictionaryButton.enabled = false;	
		}	
	}
	
	private function thesaurusFaultHandler(event:FaultEvent):void
	{
		Alert.show( ObjectUtil.toString(event.fault) );
	}

	protected function initialization():void
	{
		showSplashScreen()
	}
	
	private function showSplashScreen():void {
		img.addEventListener(MouseEvent.CLICK, img_click);
		img.addEventListener(Event.COMPLETE, img_complete);
		img.load('assets/splash/CurrencyConverterSplash.png');
		PopUpManager.addPopUp(img, this, true);
		loadTimer = new Timer(1500);
		loadTimer.addEventListener(TimerEvent.TIMER, incrementTime);
		loadTimer.start();
	}
	
	private function img_complete(evt:Event):void {
		//img = evt.currentTarget as Image;
		img.width = img.contentWidth;
		img.height = img.contentHeight;
		PopUpManager.centerPopUp(img);
	}
	
	private function img_click(evt:MouseEvent):void {
		//img = evt.currentTarget as Image;
		PopUpManager.removePopUp(img);
		clearCurrencyButton.enabled = false;	
		clearQuoteButton.enabled = false;
		clearDictionaryButton.enabled = false;
	}
	
	private function incrementTime(event:TimerEvent):void
	{
		timeElapsed++;
		
		// close the splash screen, and open the document window
		if ((LOAD_DELAY - timeElapsed) == 0)
		{
			loadTimer.stop();
			loadTimer.removeEventListener(TimerEvent.TIMER, incrementTime);
			loadTimer = null;
			
			if(img!=null){
				PopUpManager.removePopUp(img);
			}
			
			clearCurrencyButton.enabled = false;	
			clearQuoteButton.enabled = false;
			clearDictionaryButton.enabled = false;
			
		}
	}